<template>
	<view>
		<custom title="支付结果" fixed="fixed" mode="2" titleColor="#FFFFFF" backColor="#FFFFFF"
			background="rgba(255,255,255,0)">
		</custom>



		<view
			style="position: fixed;top:0;z-index: 1;width: 100vw;height: 500rpx;background: linear-gradient(180deg, #2f95fd, #F1F1F1);">
		</view>


		<view class="flex flex-y flex-x-y pr"
			style="z-index: 2;width: 686rpx;margin:  40rpx 32rpx;background-color: #FFFFFF;border-radius: 8rpx;"
			v-if="status == 1">

			<image style="transform: translateX(16rpx);margin-top:50rpx;width:80rpx;height:80rpx;"
				src="../../static/images/pay-success.png" mode=""></image>

			<view class="fs-36 color-333333 bold" style="margin-top: 32rpx;">支付成功</view>

			<view class="fs-26 color-999999" style="margin-top: 10rpx;">
				感谢你的购买
			</view>

			<view class="flex" style="margin-top: 32rpx;margin-bottom: 74rpx;">
				<view class="pay-btn" @tap="uniBack()">继续逛逛</view>
				<view class="pay-btn-1" @tap="toPage('/pages/orderDetail/orderDetail?id='+order_id,'3')"> 查看订单</view>
			</view>

		</view>
		<view class="flex flex-y flex-x-y pr"
			style="z-index: 2;width: 686rpx;margin:  40rpx 32rpx;background-color: #FFFFFF;border-radius: 8rpx;"
			v-if="status == 2 ">

			<image style="transform: translateX(16rpx);margin-top:50rpx;width:80rpx;height:80rpx;"
				src="../../static/images/pay-fail.png" mode=""></image>

			<view class="fs-36 color-333333 bold" style="margin-top: 32rpx;">支付失败</view>

			<view class="fs-26 color-999999 bold" style="margin-top: 10rpx;">
				重新支付试试
			</view>

			<view class="flex" style="margin-top: 32rpx;margin-bottom: 74rpx;">
				<view class="pay-btn" @tap="cancel_order">取消订单</view>
				<view class="pay-btn-1" @tap="pay_1">重新支付</view>
			</view>

		</view>

		<toast ref="toast"></toast>

		<paylist ref="pay"></paylist>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				status: 0, // 1成功 2 失败 0等待
				order_id: '',
				price: '',
			}
		},
		onShow() {
			// if (this.pass) {
			// 	//重新支付
			// 	this.toPage('/pages/order/order', '3');
			// }
			if (uni.getStorageSync('order_id')) {
				let pay_time = uni.getStorageSync('pay_time');
				if (new Date().getTime() - pay_time < 1800)
					return;
				let order_id = uni.getStorageSync('order_id');
				let pay_id = uni.getStorageSync('pay_id');
				uni.setStorageSync('order_id', '');
				uni.setStorageSync('pay_id', '');
				this.toPage('/pages/order/order', '3');
			}

		},
		onLoad(e) {
			// if (e.status) {
			// 	this.status = e.status;

			// }
			if (e.payid) {
				this.payid = e.payid;
			}
			if (e.order_id) {
				this.order_id = e.order_id;
				this.get_status();
				this.get_price();
			}
			if (e.type) {
				this.type = e.type;
			}
		},
		methods: {
			get_price() {
				this.request({
					url: '/userOrder/detail',
					data: {
						orderId: this.order_id,
					},
					// loading: true,
				}).then(res => {
					if (res.status == 200 && res.data) {
						this.price = res.data.totalAmount || 0;
					}
				});
			},
			get_status() {
				this.request({
					url: `/pay/findOrder/${this.order_id}`
				}).then(res => {

					if (res.status == 200) {
						// this.$refs.toast.open({
						// 	desc:JSON.stringify(res.data)
						// });

						if (this.type == 'order') {
							this.$emit('refresh_order');
						}
						if (res.data == 3) {
							this.status = 1;

						} else {
							this.status = 2;
						}
						// if (res.data == 30) {
						// 	this.status = 1;
						// }
					}
					// this.$refs.toast.open({
					// 	desc:JSON.stringify(res)
					// });
				});
			},
			cancel_order() {
				this.less(() => {
					this.request({
						url: '/userOrder/cancel',
						data: {
							orderId: this.order_id
						},
						loading: true,
					}).then(res => {
						if (res.status == 200) {
							this.$refs.toast.open({
								desc: '订单取消成功',
								left: '',
								right: "知道了",
								success: () => {
									// this.uniBack();
									this.toPage('/pages/order/order', '3');
								}
							});
						}
					});
				});
			},
			pay() {

				this.less(() => {




					this.request({
						url: `/pay/ada/${this.order_id}/${Number(this.payid)}`,
						data: {

						},
						loading: true,
					}).then(pay => {
						if (pay.status == 'succeeded') {
							this.pass = true;
							////拉起支付
							if (pay.expend && pay.expend.pay_info) {

								this.disable = true;
								uni.setStorageSync('order_id', this.order_id);
								uni.setStorageSync('pay_time', new Date().getTime());

								// #ifdef APP-PLUS 

								// 微信
								if (this.payid == 1006) {
									plus.runtime.openURL(pay.expend.pay_info, function(
										res) {});
									return;
								}

								//支付宝
								let pay_url = pay.expend.pay_info;


								var authUrl =
									"alipays://platformapi/startapp?appId=20000067&url=" +
									encodeURIComponent(pay_url);

								if (uni.getSystemInfoSync().platform == 'ios') {
									authUrl = (
										"alipay://platformapi/startapp?appId=20000067&url=" +
										encodeURIComponent(pay_url));
								}

								plus.runtime.openURL(authUrl, function(res) {
									this.showToast('请安装支付宝之后再进行支付！');
								});
								// #endif

								// #ifdef H5



								var u = navigator.userAgent;
								var isAndroid = u.indexOf('Android') > -1 || u.indexOf(
									'Adr') > -1; //android终端
								if (isAndroid) {

									window.open(pay.expend.pay_info, "_blank",
										"resizable,scrollbars,status");
								} else {

									window.location.href = pay.expend.pay_info;

								}

								// #endif


							} else {
								this.$refs.toast.open({
									desc: pay?.data?.respDesc || '请稍后重试',
								});
							}
							////

						}
					});

				});
			},

			pay_1() {

				this.$refs.pay.open({
					price: this.price,
					mine: () => {
						this.uniBack();
					},
					order_id: this.order_id,
				});
				return;

				this.less(() => {




					this.request({
						url: `/pay/${this.order_id}/${Number(this.payid)}`,
						data: {

						},
						loading: true,
					}).then(pay => {
						if (pay.status == 200) {
							this.pass = true;
							////拉起支付
							if (pay.data && pay.data.payUrl) {

								this.disable = true;
								uni.setStorageSync('order_id', this.order_id);
								uni.setStorageSync('pay_time', new Date().getTime());

								// #ifdef APP-PLUS 

								// 微信
								if (this.payid == 1006) {
									plus.runtime.openURL(pay.data.payUrl, function(
										res) {});
									return;
								}

								//支付宝
								let pay_url = pay.data.payUrl;


								var authUrl =
									"alipays://platformapi/startapp?appId=20000067&url=" +
									encodeURIComponent(pay_url);

								if (uni.getSystemInfoSync().platform == 'ios') {
									authUrl = (
										"alipay://platformapi/startapp?appId=20000067&url=" +
										encodeURIComponent(pay_url));
								}

								plus.runtime.openURL(authUrl, function(res) {
									this.showToast('请安装支付宝之后再进行支付！');
								});
								// #endif

								// #ifdef H5



								var u = navigator.userAgent;
								var isAndroid = u.indexOf('Android') > -1 || u.indexOf(
									'Adr') > -1; //android终端
								if (isAndroid) {

									window.open(pay.data.payUrl, "_blank",
										"resizable,scrollbars,status");
								} else {

									window.location.href = pay.data.payUrl;

								}

								// #endif


							} else {
								this.$refs.toast.open({
									desc: pay?.data?.respDesc || '请稍后重试',
								});
							}
							////

						}
					});

				});
			},

		}
	}
</script>

<style>
	page {
		page {
			/* background: linear-gradient(180deg, #2f95fd, #F1F1F1); */
			background: #F1F1F1;
		}
	}

	.pay-btn {
		font-size: 32rpx;
		color: #999999;
		width: 280rpx;
		height: 88rpx;
		border-radius: 8rpx;

		display: flex;
		justify-content: center;
		align-items: center;
		border: 2rpx solid #999999;
	}

	.pay-btn-1 {
		font-size: 32rpx;
		color: #2F95FD;
		width: 280rpx;
		height: 88rpx;
		border-radius: 8rpx;

		margin-left: 28rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border: 2rpx solid #2f95fd;
	}
</style>
